#pragma once

#include "base.h"
#include "allocation.h"

namespace cgen {
namespace internal {



// StatsCounters are dynamically created values which can be tracked in
// the StatsTable.  They are designed to be lightweight to create and
// easy to use.
//
// Internally, a counter represents a value in a row of a StatsTable.
// The row has a 32bit value for each process/thread in the table and also
// a name (stored in the table metadata).  Since the storage location can be
// thread-specific, this class cannot be shared across threads.
//
// This class is designed to be POD initialized.  It will be registered with
// the counter system on first use.  For example:
//   StatsCounter c = { "c:myctr", NULL, false };
struct StatsCounter
{
    int m_counter;

    StatsCounter()
            : m_counter(0)
    {}

    void Increment()
    { ++m_counter; }

    void Decrement()
    { --m_counter; }

    void Increment(int value)
    { m_counter += value; }

    void Decrement(int value)
    { m_counter -= value; }
};


// WARNING: STATS_COUNTER_LIST_* is a very large macro that is causing MSVC
// Intellisense to crash.  It was broken into two macros (each of length 40
// lines) rather than one macro (of length about 80 lines) to work around
// this problem.  Please avoid using recursive macros of this length when
// possible.
#define STATS_COUNTER_LIST_1(SC)                                 \
  /* Global Handle Count*/                                       \
  SC(global_handles, V8.GlobalHandles)                           \
  /* Mallocs from PCRE */                                        \
  SC(pcre_mallocs, V8.PcreMallocCount)                           \
  /* OS Memory allocated */                                      \
  SC(memory_allocated, V8.OsMemoryAllocated)                     \
  SC(props_to_dictionary, V8.ObjectPropertiesToDictionary)       \
  SC(elements_to_dictionary, V8.ObjectElementsToDictionary)      \
  SC(alive_after_last_gc, V8.AliveAfterLastGC)                   \
  SC(objs_since_last_young, V8.ObjsSinceLastYoung)               \
  SC(objs_since_last_full, V8.ObjsSinceLastFull)                 \
  SC(symbol_table_capacity, V8.SymbolTableCapacity)              \
  SC(number_of_symbols, V8.NumberOfSymbols)                      \
  /* Current amount of memory in external string buffers. */     \
  SC(total_external_string_memory, V8.TotalExternalStringMemory) \
  SC(script_wrappers, V8.ScriptWrappers)                         \
  SC(call_initialize_stubs, V8.CallInitializeStubs)              \
  SC(call_premonomorphic_stubs, V8.CallPreMonomorphicStubs)      \
  SC(call_normal_stubs, V8.CallNormalStubs)                      \
  SC(call_megamorphic_stubs, V8.CallMegamorphicStubs)            \
  SC(arguments_adaptors, V8.ArgumentsAdaptors)                   \
  SC(compilation_cache_hits, V8.CompilationCacheHits)            \
  SC(compilation_cache_misses, V8.CompilationCacheMisses)        \
  SC(regexp_cache_hits, V8.RegExpCacheHits)                      \
  SC(regexp_cache_misses, V8.RegExpCacheMisses)                  \
  /* Amount of evaled source code. */                            \
  SC(total_eval_size, V8.TotalEvalSize)                          \
  /* Amount of loaded source code. */                            \
  SC(total_load_size, V8.TotalLoadSize)                          \
  /* Amount of parsed source code. */                            \
  SC(total_parse_size, V8.TotalParseSize)                        \
  /* Amount of source code skipped over using preparsing. */     \
  SC(total_preparse_skipped, V8.TotalPreparseSkipped)            \
  /* Amount of compiled source code. */                          \
  SC(total_compile_size, V8.TotalCompileSize)


#define STATS_COUNTER_LIST_2(SC)                                    \
  /* Number of code stubs. */                                       \
  SC(code_stubs, V8.CodeStubs)                                      \
  /* Amount of stub code. */                                        \
  SC(total_stubs_code_size, V8.TotalStubsCodeSize)                  \
  /* Amount of (JS) compiled code. */                               \
  SC(total_compiled_code_size, V8.TotalCompiledCodeSize)            \
  SC(gc_compactor_caused_by_request, V8.GCCompactorCausedByRequest) \
  SC(gc_compactor_caused_by_promoted_data,                          \
     V8.GCCompactorCausedByPromotedData)                            \
  SC(gc_compactor_caused_by_oldspace_exhaustion,                    \
     V8.GCCompactorCausedByOldspaceExhaustion)                      \
  SC(gc_compactor_caused_by_weak_handles,                           \
     V8.GCCompactorCausedByWeakHandles)                             \
  SC(gc_last_resort_from_js, V8.GCLastResortFromJS)                 \
  SC(gc_last_resort_from_handles, V8.GCLastResortFromHandles)       \
  /* How is the generic keyed-load stub used? */                    \
  SC(keyed_load_generic_smi, V8.KeyedLoadGenericSmi)                \
  SC(keyed_load_generic_symbol, V8.KeyedLoadGenericSymbol)          \
  SC(keyed_load_generic_slow, V8.KeyedLoadGenericSlow)              \
  /* Count how much the monomorphic keyed-load stubs are hit. */    \
  SC(keyed_load_function_prototype, V8.KeyedLoadFunctionPrototype)  \
  SC(keyed_load_string_length, V8.KeyedLoadStringLength)            \
  SC(keyed_load_array_length, V8.KeyedLoadArrayLength)              \
  SC(keyed_load_constant_function, V8.KeyedLoadConstantFunction)    \
  SC(keyed_load_field, V8.KeyedLoadField)                           \
  SC(keyed_load_callback, V8.KeyedLoadCallback)                     \
  SC(keyed_load_interceptor, V8.KeyedLoadInterceptor)               \
  SC(keyed_load_inline, V8.KeyedLoadInline)                         \
  SC(keyed_load_inline_miss, V8.KeyedLoadInlineMiss)                \
  SC(named_load_inline, V8.NamedLoadInline)                         \
  SC(named_load_inline_miss, V8.NamedLoadInlineMiss)                \
  SC(named_load_global_inline, V8.NamedLoadGlobalInline)            \
  SC(named_load_global_inline_miss, V8.NamedLoadGlobalInlineMiss)   \
  SC(keyed_store_field, V8.KeyedStoreField)                         \
  SC(keyed_store_inline, V8.KeyedStoreInline)                       \
  SC(keyed_store_inline_miss, V8.KeyedStoreInlineMiss)              \
  SC(named_store_global_inline, V8.NamedStoreGlobalInline)          \
  SC(named_store_global_inline_miss, V8.NamedStoreGlobalInlineMiss) \
  SC(call_global_inline, V8.CallGlobalInline)                       \
  SC(call_global_inline_miss, V8.CallGlobalInlineMiss)              \
  SC(constructed_objects, V8.ConstructedObjects)                    \
  SC(constructed_objects_runtime, V8.ConstructedObjectsRuntime)     \
  SC(for_in, V8.ForIn)                                              \
  SC(enum_cache_hits, V8.EnumCacheHits)                             \
  SC(enum_cache_misses, V8.EnumCacheMisses)                         \
  SC(reloc_info_count, V8.RelocInfoCount)                           \
  SC(reloc_info_size, V8.RelocInfoSize)                             \
  SC(zone_segment_bytes, V8.ZoneSegmentBytes)                       \
  SC(compute_entry_frame, V8.ComputeEntryFrame)


class Counters : public AllStatic
{
public:

    // stats counters
#define SC(name, caption) static StatsCounter name;

    STATS_COUNTER_LIST_1(SC)
    STATS_COUNTER_LIST_2(SC)

#undef SC

private:
    DISALLOW_IMPLICIT_CONSTRUCTORS(Counters);
};


}} // namespace cgen::internal
